<?php
namespace app\admin\controller;

use app\admin\model\Administrator;

class Admin extends Base
{
    /**
     * 管理员列表
     * @return mixed
     */
    public function index(){
        $where = [];
        //若提交参数
        if ($this->request->isPost()){
            $param = $this->request->param();
            if ($param['username']){
                $where[] = ['username', 'like', '%'.$param['username'].'%'];//todo:后期优化like查询
            }
            if ($param['start_time']){
                $where[] = ['create_time', '>=', strtotime($param['start_time'])];
            }
            if ($param['end_time']){
                $where[] = ['create_time', '<', strtotime($param['end_time'])];
            }
        }
        //查询管理员信息
        $administrator = new Administrator();
        $list = $administrator->getDetail($where);

        $this->assign('list', $list);
        return$this->fetch();
    }

    /**
     * 新增管理员
     * @return array|mixed
     */
    public function add(){
        //若是新增操作
        if ($this->request->isPost()){
            $param = $this->request->param();
            //判断
            $administrator = new Administrator($param);
            if ($administrator->checkExist($param['username'])){
                $this->error('该账号已存在！');
            }
            //插入
            $result = $administrator->allowField(true)->save();
            if ($result){
                $this->success('新增成功。');
            }
            $this->error('新增失败！');
        }
        return $this->fetch();
    }

    /**
     * 编辑管理员信息
     * @return mixed
     */
    public function edit(){
        $param = $this->request->param();
        $administrator = new Administrator();
        //更新操作
        if ($this->request->isPost()){
            $result = $administrator->allowField(true)->save($param, ['id'=>$param['id']]);
            if ($result !== false){
                $this->success('更新成功。');
            }
            $this->error('更新失败！');
        }
        //获取管理员信息
        $detail = $administrator->getDetailById($param['id']);

        $this->assign('detail', $detail);
        return $this->fetch();
    }

    /**
     * 删除管理员
     */
    public function delete(){
        $id = $this->request->param('id');
        //判断
        if ($id == config('super_administrator')){
            $this->error('禁止删除超级管理员！');
        }
        //todo:是否有删除权限
        //软删除 //todo:新建管理员的时候可能有重名
        $administrator = new Administrator();
        $result = $administrator->changeStatus($id, -1);
        if ($result){
            $this->success('删除成功。');
        }
        $this->error('删除失败！');
    }

    public function changeStatus(){
        $param = $this->request->only(['id', 'status']);
        //判断
        if (!$param['id'] || !is_numeric($param['status'])){
            $this->error('非法参数！');
        }
        //修改状态
        $administrator = new Administrator();
        $result = $administrator->changeStatus($param['id'], $param['status']);
        if ($result){
            $this->success('操作成功。');
        }
        $this->error('操作失败！');
    }

}